@extends('admin.layout.layout')
@section('content')
        <div class="x-nav">
            <span class="layui-breadcrumb">
                <a href="">首页</a>
                <a>
                    <cite>分类列表</cite></a>
            </span>
            <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right" onclick="location.reload()" title="刷新">
                <i class="layui-icon layui-icon-refresh" style="line-height:30px"></i>
            </a>
        </div>
        <div class="layui-fluid">
            <div class="layui-row layui-col-space15">
                <div class="layui-col-md12">
                    <div class="layui-card">
                        <div class="layui-card-header">
                            <button class="layui-btn layui-btn-danger" onclick="delAll()">
                                <i class="layui-icon"></i>批量删除</button>
                            <button class="layui-btn" onclick="xadmin.open('添加分类','{{url("admin/category/create")}}',600,400)">
                                <i class="layui-icon"></i>添加
                            </button>
                        </div>
                        <div class="layui-card-body ">
                            <table class="layui-table layui-form" id="tree-table"></table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <script>
        layui.config({
            base: '/style/admin/js/'
        });
          layui.use(['treeTable','layer','code','form'], function(){
              var o = layui.$,
                  form = layui.form,
                  layer = layui.layer,
                  treeTable = layui.treeTable;
              treeTable.render({
                  elem: '#tree-table',
                  data:{!! $cates !!},
                  icon_key: 'cate_name',
                  primary_key: 'cate_id',
                  parent_key: 'cate_pid',
                  is_checkbox: true,
                  end: function(e){
                      form.render();
                  },
                  cols: [
                      {
                          key: 'cate_id',
                          title: '分类id',
                          align: 'center'
                      },
                      {
                          key: 'cate_name',
                          title: '分类名称',
                      },
                      {
                          key: 'cate_title',
                          title: '分类别名',
                          align: 'center'
                      },
                      {
                          key: 'cate_order',
                          title: '排序',
                          align: 'center',
                          width:"100px",
                          template: function(item){
                              return '<input  cate_id="'+item.cate_id+'" type="text" name="orderid" class="layui-input" value="'+item.cate_order+'">';
                          }
                      },
                      {
                          key: 'cate_pid',
                          title: '父id',
                          align: 'center'
                      },
                      {
                          title: '操作',
                          align: 'center',
                          template: function(item){
                              return '<a lay-filter="add" href="javascript:;">新增下级栏目</a> | <a  href="javascript:;" lay-filter="edit">编辑</a>';
                          }
                      }
                  ]
              });
              //排序
              o("#tree-table").on("change",'input',function () {
                  var cate_id = o(this).attr('cate_id');
                  var cate_order = o(this).val();
                    o.post('{{url("admin/category/checkorder")}}',{'_token':'{{csrf_token()}}',cate_id:cate_id,cate_order:cate_order},function (res) {
                        console.log(res.status);
                        if(res.status === 0){
                            //发异步，把数据提交给php
                            layer.alert(res.message, {
                                    icon: 6
                                },
                                function() {
                                    location.reload();
                                });
                        }else {
                            layer.msg(res.data[0]);
                        }
                    },'json');
              });
              // 监听自定义
              treeTable.on('tree(add)',function(data){
                  xadmin.open('新增下一级','{{url("admin/category/create?cate_id=")}}'+data.item.cate_id,600,400);
              });
              treeTable.on('tree(edit)',function(data){
                  xadmin.open('新增下一级','/admin/category/'+data.item.cate_id+'/edit',600,400);
              })
          });

           /*用户-删除*/
          function member_del(obj,id){
              layer.confirm('确认要删除吗？',function(index){
                  //发异步删除数据
                  $(obj).parents("tr").remove();
                  layer.msg('已删除!',{icon:1,time:1000});
              });
          }
        </script>
@endsection
